<?php
defined('JOC') or die;

/**
 * Description of nav
 * 
 * @author jcamelis
 * 
 * Depends : 
 *    core/module/module.class.php
 *    core/module/form.class.php
 *    core/db/db.php
 */

class nav extends Module
{
  public  $id ;
  public  $permanent_link 	= '' ;
  public  $arrIds    				= Array() ;
  public  $_elements 				= Array() ;
  private $_module_instance = Array() ;
  public  $module_id 				= 5 ;

  //Database table constants
  protected $_table_name  = 'nav' ;
  protected $_pk_name     = 'nav_id' ;
  
  function __construct($module_instance = Array())
  {
 // parent::__construct();
    $this->_module_instance = $module_instance ;
    return $this ;
  }
  function exists_Elements( $params = Array() )
  {
//  	debug::pr($params) ;
  	$level 		= isset($params['level']) 		? $params['level'] 		: 0 ;
  	$paren_id = isset($params['paren_id']) 	? $params['paren_id'] : 0 ;
  	$count = 0 ;
  	foreach($this->_elements as $element)
  	{
  		if($element['level'] == $level && $element['parent_id'] == $parent_id )
  		{
  			$count++ ;
  		}
  	}
//  	debug::pr($count) ;

  	return $count ? Array( 0 => Array( 'level' => $level ) ) : Array() ;
  }
	function get_Elements( $params = Array() )
  {
  	$level     = isset($params['level'])      ? $params['level']     : 0 ;
  	$parent_id = isset($params['parent_id'])  ? $params['parent_id'] : 0 ;
  	 
  	$tmp_elements = Array() ;
  	
  	foreach($this->_elements as $element)
  	{
  		if($element['level'] == $level && $element['parent_id'] == $parent_id )
  		{
  			$tmp_elements[] = $element ;
  		}
  	}
  	return $tmp_elements ;
  }
/*
 * here begins the CRUD method 
*/
  function crud( $method = 'read',$sub_method = 'simple' )
  {
    $_to_be_call = "_{$$method}_{$sub_method}";
    return method_exists(__CLASS__,$_to_be_call) ? $this->$_to_be_call() : false ;
  }
/* CREATE */
  function _create_simple()
  {
    return $this ;
  }
  function _create_multiple()
  {
    return $this ;
  }
/* READ */
  function _read_simple()
  {
    return $this ;
  }
  function _read_multiple()
  {
  	global $db ;
  	$sql = " SELECT N.* , MIC.`level` ,MIC.`parent_id` " ;
		$sql .= " FROM `{prefix}module_instance_content` AS MIC " ;
		$sql .= " INNER JOIN `{prefix}nav` AS N ON MIC.`content_id` = N.`nav_id` " ;
		$sql .= " WHERE MIC.`module_instance_id` = {$this->_module_instance['module_instance_id']} AND MIC.`content_module_id` = {$this->module_id} ORDER BY MIC.`order` ASC ; " ;

    $this->_elements = $db->query($sql) ;
  }
/* UPDATE */
  function _update_simple()
  {
    return $this ;
  }
  function _update_multiple()
  {
    return $this ;
  }
/* DELETE */
  function _delete_simple()
  {
    return $this ;
  }
  function _delete_multiple()
  {
    return $this ;
  }
  function __destruct()
  {
    return $this ;
  }
}